<template>
  <div class='table'>
    <ui-container style="width:100%">
      <div class='t_top'>
        <el-select v-model="type" placeholder="优惠券类型" size="small">
          <el-option label="全部" value="null"></el-option>
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <el-input
          placeholder="店铺优惠券编号"
          v-model="ShopCouponNumber"
          size="small"
          clearable style="width:300px">
        </el-input>
        <el-input
          placeholder="手机号"
          v-model="Phone"
          size="small"
          clearable style="width:200px">
        </el-input>
        <el-button type="primary" size="small" @click="search()" >查询</el-button>
      </div>
    </ui-container>
    <ui-container style='margin-top:15px' v-loading="loading">
      <div style="text-align:right;" class="btn-row">
        <el-button type="success" size="small" @click="enable">核销</el-button>
      </div>
      <div class='t_bottom' style="width:100%; font-size:14px;">
        <el-table
          ref='multipleTable'
          :data='tableData'
          tooltip-effect='light'
          style='width:100%'
          border
        >
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column prop='ShopCouponNumber' label='店铺优惠券编号' width='180' ></el-table-column>
          <el-table-column prop='TypeStr' label='优惠券类型' width='140'></el-table-column>  
          <el-table-column prop='CouponAmount' label='金额'></el-table-column>
          <el-table-column prop='FullAmount' label='使用条件' width='140'>
            <template slot-scope="scope">
              <span v-if="scope.row.FullAmount == 0">消费金额满{{scope.row.FullAmount}}元</span>
              <span v-else>无限制</span>
            </template>
          </el-table-column>
          <el-table-column prop='TypeStr' label='有效期' width='300'>
            <template slot-scope="scope">
              <span>{{scope.row.StartTimeStr}} - {{scope.row.EndTimeStr}}</span>
            </template>
          </el-table-column>  
          <el-table-column prop='UserNumber' label='所属会员' width='200'></el-table-column>
          <el-table-column prop='CreateTimeStr' label='领取时间' width='160'></el-table-column>
          <el-table-column prop='IsUse' label='是否使用' width='100'>
            <template slot-scope="scope">
              <span>{{scope.row.IsUse?'是':'否'}}</span>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-row">
          <el-pagination
            background
            layout="total,prev, pager, next,sizes"
            :total="totalNum">
          </el-pagination>
        </div>
      </div>
    </ui-container>
  </div>
</template>
<script type='text/javascript'>
import {cookieStorage} from 'src/common/storage'
export default {
  name: 'shopCouponList',
  data () {
    return {
      typeList:[{
          value: 0,
          label: '满减优惠券'
        }, {
          value: 1,
          label: '代金券'
        }],
      ShopCouponNumber:'',
      Phone: '',
      type: null,
      loading: false,
      tableData: [],
      totalNum:0,
      pageIndex:1,
      pageSize:10,
      multipleSelection:''
  }
  },
  mounted () {
     this.init();
  },
  methods: {
    search(){
      this.pageIndex = 1;
      this.init();
    },
    init(){
      this.loading=true;
      var data = {
          ShopCouponNumber: this.ShopCouponNumber,
          Type: eval(this.type),
          Phone: this.Phone,
          PageIndex: this.pageIndex,
          PageSize: this.pageSize
        };

      this.$api.shop.list(data).then((rs) => {
        console.log(rs)
        for(let i=0;i<rs.Data.Data.length;i++){
          rs.Data.Data[i].isSelect = false;
        }
        this.tableData = rs.Data.Data;
        this.totalNum = rs.Data.TotalNum;
        this.loading=false;
      }).catch((res) =>{this.loading=false;})
    },
    enable() {
      let selected = this.$refs.multipleTable.selection;
      if (selected.length !== 1) {
          this.$message.error("请选择一条数据重置密码！");
          return;
      }
      let ids = [];
      ids = ids.concat(selected.map(m => m.ShopCouponNumber));
      let index = ids[0];
      this.$confirm('核销该优惠券?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.shop.enable(index).then((rs) => {
        if(rs.IsSuccess){
          this.$message({
            type: 'success',
            message: '核销成功'
          });
          this.init();
          }
        }).catch((res) =>{})
      }).catch(() => {
        // this.$message({
        //   type: 'info',
        //   message: '已取消删除'
        // });
      });
    },
  }
}
</script>
<style lang='scss' type='text/scss' rel='stylesheet/scss'>
.container-main{
  width: 100% !important;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .btn-row{
    margin-bottom:10px;
  }
  .btn-row .el-button+.el-button{
    margin-left:0px;
  }
</style>
